﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Xml;
using Maticsoft.DBUtility;
using System.IO;//请先添加引用
public partial class ParseXML : System.Web.UI.Page
{
    XmlDocument doc = new XmlDocument();
    TaskModel taskModel = new TaskModel();
    protected void Page_Load(object sender, EventArgs e)
    {
        Stream stream = Request.InputStream;
        StreamReader sr = new StreamReader(stream);

        string content = sr.ReadToEnd();               
        doc.LoadXml(content);
        try
        {
            doc.Save(@"C:\" + DateTime.Now.Year + DateTime.Now.Month + DateTime.Now.Day + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second + DateTime.Now.Millisecond + ".xml");
        }
        catch (Exception ex)
        {

        }        
        TraverseTask(doc);
        UpdateData();
        //taskModel中加入sql语句即可！ ^.^
    }

    private int UpdateData()
    {
        int updateCount = 0;
        foreach (OverLine overLine in taskModel.overLineList)
        {
            updateCount += DbHelperOra.ExecuteSql(overLine.getUpdateSQL());
        }
        foreach (Tower tower in taskModel.towerList)
        {
            updateCount += DbHelperOra.ExecuteSql(tower.getUpdateSQL());
        }
        foreach (Device device in taskModel.deviceList)
        {
            updateCount += DbHelperOra.ExecuteSql(device.getUpdateSQL());
        }
        return updateCount;
    }
    private void TraverseTask(XmlDocument doc){
        TraverseOverLine(doc);
        TraverTower(doc);
        TraverDevice(doc);
    }

    private void TraverseOverLine(XmlDocument doc)
    {
        XmlNodeList nodeList = doc.SelectNodes("//杆塔/线路[@action!='0']"); //线路
        OverLine overLine;
        int id;
        string name;
        foreach (XmlNode node in nodeList)
        {
            if (node.Attributes["action"].Value.ToString() == "1")
            {
                id = Int32.Parse(node.Attributes["id"].Value.ToString());
                name = node.Attributes["name"].Value.ToString();

                overLine = new OverLine();
                overLine.lineID = id;
                overLine.lineName = name;

                taskModel.addLineInfo(overLine);
            }
        }
    }
    private void TraverTower(XmlDocument doc){
        XmlNodeList nodeList = doc.SelectNodes("//杆塔[@action!='0']"); //杆塔
        Tower tower;
        int id;
        string name;
        double x;
        double y;
        double z;
        foreach (XmlNode node in nodeList)
        {
            if (node.Attributes["action"].Value.ToString() == "1")
            {
                id = Int32.Parse(node.Attributes["id"].Value.ToString());
                //name = node.Attributes["name"].Value.ToString();
                x = Double.Parse(node.Attributes["x"].Value.ToString());
                y = Double.Parse(node.Attributes["y"].Value.ToString());
                z = Double.Parse(node.Attributes["z"].Value.ToString());

                tower = new Tower();
                tower.towerID = id;
                //tower.towerName = name;
                tower.x = x;
                tower.y = y;
                tower.z = z;

                taskModel.addTowerInfo(tower);
            }
        }
    }
    private void TraverDevice(XmlDocument doc)
    {
        XmlNodeList nodeList = doc.SelectNodes("//设备[@action!='0']"); //设备
        Device device;
        int id;
        string name;
        string specs;
        int amount;
        foreach (XmlNode node in nodeList)
        {
            if (node.Attributes["action"].Value.ToString() == "1")
            {
                id = Int32.Parse(node.Attributes["id"].Value.ToString());
                name = node.Attributes["name"].Value.ToString();
                specs = node.Attributes["specs"].Value.ToString();
                amount = 0;
                Int32.TryParse(node.Attributes["amount"].Value.ToString(),out amount);

                device = new Device();
                device.deviceID = id;
                device.deviceName = name;
                device.specs = specs;
                device.amount = amount;

                taskModel.addDeviceInfo(device);
            }
            else if (node.Attributes["action"].Value.ToString() == "2")
            {
                id = -1;
                int belongtower = int.Parse(node.ParentNode.ParentNode.Attributes["id"].Value);
                int belongline = int.Parse(node.ParentNode.Attributes["id"].Value); 
                name = node.Attributes["name"].Value.ToString();
                specs = node.Attributes["specs"].Value.ToString();
                amount = Int32.Parse(node.Attributes["amount"].Value.ToString());

                device = new Device();
                device.deviceID = id;
                device.deviceName = name;
                device.specs = specs;
                device.amount = amount;
                device.belongLine = belongline;
                device.belongTower = belongtower;

                taskModel.addDeviceInfo(device);
            }
        }
    }
    private class TaskModel{
        public ArrayList overLineList = new ArrayList();
        public ArrayList towerList = new ArrayList();
        public ArrayList deviceList = new ArrayList();

        public int addLineInfo(OverLine line){
            return overLineList.Add(line);
        }
        public int addTowerInfo(Tower tower)
        {
            return towerList.Add(tower);
        }
        public int addDeviceInfo(Device device)
        {
            return deviceList.Add(device);
        }
    }
    private class OverLine{
        public string lineName = "";
        public int lineID = 0;

        public string getUpdateSQL(){
            string sql = "update tab_overline set LINENAME='" + lineName + "' where OVERLINEID=" + lineID;
            return sql;//可以返回组装好的update语句
        }

    }
    private class Tower{
        public string towerName = "";
        public int towerID = 0;
        public double x = 0;
        public double y = 0;
        public double z = 0;

        public string getUpdateSQL()
        {
//Name	Code	Data Type	Domain	Mandatory	Primary Identifier	Displayed
//ID	TowerID	Serial (25)	<None>	TRUE	TRUE	TRUE

//经度	Longitude	Decimal (25)	<None>	FALSE	FALSE	TRUE
//纬度	Latitude	Decimal (25)	<None>	FALSE	FALSE	TRUE

//杆塔名称	TowerName	Variable characters (50)	<None>	TRUE	FALSE	TRUE
// ELEVATION
            string sql = "update tab_tower set Longitude=" + x + ",Latitude=" + y + ",ELEVATION=" + z + "  where TowerID = " + towerID;
            return sql;//可以返回组装好的update语句
        }
    }
    private class Device{
        public int belongLine;
        public int deviceID = 0;
        public string deviceName = "";
        public int belongTower;
        public string specs = "";
        public int amount = 0;

        public string getUpdateSQL()
        {
            //DEVICEID	NUMBER(25)	N			
            //BELONGUNIT	VARCHAR2(50)	Y			
            //BELONGLINE	VARCHAR2(50)	Y			
            //DEVICENAME	VARCHAR2(100)	Y			
            //VOLTAGEGRADE	VARCHAR2(50)	Y			
            //SPECIALTYPE	VARCHAR2(50)	Y			
            //DEVICECLASS	VARCHAR2(50)	Y			
            //DEVICETYPE	VARCHAR2(50)	Y			
            //NAMEPLATE	VARCHAR2(50)	Y			
            //FACTORY	VARCHAR2(100)	Y			
            //MANUFACTURER	VARCHAR2(100)	Y			
            //RUNDATE	DATE	Y			
            //BELONGTOWER	VARCHAR2(50)	Y			
            //DEVICENUM	NUMBER(25)	Y		
            if (deviceID != -1)
            {
                string sql = "update tab_device set DEVICENAME='" + deviceName + "',DEVICETYPE='" + specs + "',DEVICENUM=" + amount + "  where DEVICEID = " + deviceID;
                return sql;//可以返回组装好的update语句
            }
            else
            {
                string sql = "insert into tab_device (DEVICENAME,DEVICETYPE,DEVICENUM,BELONGTOWER,BELONGLINE) values('" + deviceName + "','" + specs + "'," + amount +"," + belongTower + ","+belongLine+")";
                return sql;//可以返回组装好的update语句
            }
        }
    }
}
